package sepal;

import struct.CtxServiceConfig;
import struct.InvServiceConfig;
import struct.ServiceConfig;
import org.apache.log4j.PropertyConfigurator;
import config.Configuration;
import appmgr.AppMgrThread;
import resource.ResMgrThread;
import service.ctx.ctxServer.PlatformCtxServer;
import service.inv.PlatformInvServer;
import struct.enums.CmdType;
import struct.enums.ServiceType;
import util.Util;
import service.SerMgrThread;

import java.io.File;
import java.io.InputStream;
import java.util.Properties;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import  org.apache.commons.logging.Log;
import  org.apache.commons.logging.LogFactory;

public class Sepal {
    private static final Log logger = LogFactory.getLog(Sepal.class);
    private static ResMgrThread resMgr;
    private static SerMgrThread serMgr;
    private static AppMgrThread appMgr;
    private static final Lock mgrStartFlagLock = new ReentrantLock();
    private static int mgrStartFlag = 0;

    public static void main(String[] args) {
        Init();
        Start();
    }

    public static void Start() {
        serMgr.start();
        lockUntilMgrStartFlagEqual(1);
        logger.info("SerMgrThread started");
        resMgr.start();
        lockUntilMgrStartFlagEqual(2);
        logger.info("ResMgrThread started");
        appMgr.start();
        lockUntilMgrStartFlagEqual(3);
        logger.info("AppMgrThread started");
    }

    public static void incrMgrStartFlag() {
        mgrStartFlagLock.lock();
        mgrStartFlag++;
        mgrStartFlagLock.unlock();
    }

    public static void lockUntilMgrStartFlagEqual(int value) {
        while (true) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            mgrStartFlagLock.lock();
            if (mgrStartFlag == value) {
                mgrStartFlagLock.unlock();
                break;
            }
            mgrStartFlagLock.unlock();
        }
    }

    public static void Init() {
        Util.configLog4j();

        File dir = new File("output/log/");
        Util.deleteDir(dir);
        dir.mkdirs();
        dir = new File("output/");
        Util.deleteDir(dir);
        dir.mkdirs();

        //read config file
        Configuration.init();

        //init mgr
        resMgr = ResMgrThread.getInstance();
        appMgr = AppMgrThread.getInstance();
        serMgr = SerMgrThread.getInstance();
    }

    public static boolean call(String appName, ServiceType type, CmdType cmd, ServiceConfig config) {
        boolean ret = false;
        switch (type) {
            case CTX:
                ret = PlatformCtxServer.call(appName, cmd, (CtxServiceConfig) config);
                break;
            case INV:
                ret = PlatformInvServer.call(appName, cmd, (InvServiceConfig) config);
                break;
            case ALL:
                ret = PlatformCtxServer.call(appName, cmd, (CtxServiceConfig) config);
                ret = PlatformInvServer.call(appName, cmd, (InvServiceConfig) config) && ret;
                break;
        }
        return ret;
    }
}
